package Classroom2;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadSocket implements  Runnable{
    private Socket s;

    public ThreadSocket(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        try {
            //接受客户端那边的数据
            BufferedInputStream bis  = new BufferedInputStream(s.getInputStream());
            PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
            pw.println("欢迎光临！请上传文件(文本文件)！");
            pw.close();


            //将数据传入服务器的本地
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Day16\\"+ UUID.randomUUID().toString() +".jpg"));
            byte [] bytes = new byte[1024*8];
            int len;
            while ((len = bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
                bos.flush();
            }
            //释放本地资源
            bos.close();
            //回写数据  打印流
            PrintWriter pw1 = new PrintWriter(s.getOutputStream(),true);
            pw1.println("上传成功");
            pw1.close();
            //释放资源
            s.close();
        } catch (IOException e) {
        }
    }
}
